package br.com.sysdrive.controller.Usuario;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import br.com.sysdrive.DAO.UsuarioDAOImpl;
import br.com.sysdrive.model.Usuario;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author WillianFrias
 */
public class LogarUsuario extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String login = request.getParameter("loginusuario");
        String senha = request.getParameter("senhausuario");
        Usuario usuario = null;
        String mensagem2 = "Usuário/Senha Inválido! Verifique os dados digitados!";

        if (request.getParameter("acao").equals("logon")) {
            if (!login.equals("") && !senha.equals("")) {
                try {
                    UsuarioDAOImpl dao = new UsuarioDAOImpl();
                    usuario = dao.logarUsuario(login, senha);

                    if (usuario != null) {
                        HttpSession session = request.getSession(true);
                        session.setAttribute("idusuario", usuario.getIdUsuario());
                        session.setAttribute("nomeusuario", usuario.getNomeUsuario());
                        mensagem2 = "Seja Bem Vindo Sr.(a) " + usuario.getNomeUsuario() + "!";
                        session.setAttribute("mensagem", mensagem2);
                        session.setAttribute("tipousuario", usuario.getTipoUsuario().toUpperCase());

                        if (usuario.getTipoUsuario().equals("C")) {
                            request.getRequestDispatcher("Cliente/paginaprincipalcliente.jsp").forward(request, response);
                        } else if (usuario.getTipoUsuario().equals("F")) {
                            request.getRequestDispatcher("Funcionario/paginaprincipalfuncionario.jsp").forward(request, response);
                        } else if (usuario.getTipoUsuario().equals("A")) {
                            request.getRequestDispatcher("Administrador/paginaprincipaladministrador.jsp").forward(request, response);
                        } else if (usuario.getTipoUsuario().equals("FO")) {
                            request.getRequestDispatcher("Fornecedor/paginaprincipalfornecedor.jsp").forward(request, response);
                        }
                    } else {
                        request.setAttribute("mensagem2", mensagem2);
                        request.getRequestDispatcher("index.jsp").forward(request, response);
                    }
                } catch (Exception ex) {
                    System.out.println("Problemas ao logar Usuário! Erro: " + ex.getMessage());
                    ex.printStackTrace();
                }
            } else {
                request.setAttribute("mensagem2", mensagem2);
                request.getRequestDispatcher("index.jsp").forward(request, response);
            }
        } else if (request.getParameter("acao").equals("logout")) {
            HttpSession session = request.getSession(true);
            session.invalidate();

            response.setHeader("Cache-Control", "no-cache");
            response.setHeader("Pragma", "no-cache");
            response.setDateHeader("Expires", 0);
            response.setHeader("Cache-Control", "no-store");

            request.getRequestDispatcher("index.jsp").forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
